<template>
  <div>
    <div>
        <el-input 
        class="addPosInput" 
        placeholder="添加职位..." 
        suffix-icon="el-icon-circle-plus-outline" 
        v-model="pos.name"
        @keydown.enter.native="addPosition"
        size="small">
        </el-input>
       <el-button size="small" icon="el-icon-plus" type="primary" @click="addPosition">添加职称</el-button>
      <el-button type="danger" size="small" icon="el-icon-delete" :disabled="this.multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
    </div>
    <div class="posManaMain">
    <el-table 
    :data="positions" 
    style="width: 70%" 
    stripe border 
    size="small" 
    @selection-change="handleSelectionChange"
    >
    <!-- 批量删除 -->
    <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="编号" width="55">
      </el-table-column>
      <el-table-column prop="name" label="职位" width="120">
      </el-table-column>
      <el-table-column prop="createDate" label="创建时间" width="200">
      </el-table-column>
      <el-table-column prop="enabled" label="是否启用" width="150">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
          <el-tag type="danger" v-if="!scope.row.enabled">未启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column  label="操作" >
        <template slot-scope="scope">
          <el-button size="mini" @click="shoEditView(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="30%" >
      <div>
        <el-tag>职位名称</el-tag>
        <el-input v-model="updatePos.name" placeholder="请输入职位名称" size="small" class="updatePosInput"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>

    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pos: {
          name : ''
      },
      positions: [],
      dialogVisible: false,
      updatePos: {
        name: '',
        id: '',
        createDate : ''
      },
      multipleSelection: []
    }
  },
  methods: {
    initPosition() {
      this.getRequest('/system/basic/pos/').then(resp => {
         this.positions = resp
      })
    },
    // 编辑
    shoEditView(index, row) {
      // console.log(index, row);
      this.dialogVisible = true;
      // 这里绑定了数据会变换
      // this.updatePos = row;
      // 数据拷贝
      Object.assign(this.updatePos, row);
      // console.log(row);
    },
    // 删除
    handleDelete(index, row) {
      // console.log(index, row);
      this.$confirm('此操作将永久删除['+row.name+']职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        this.deleteRequest('/system/basic/pos/' + row.id).then(resp => {
          if (resp) {
            this.initPosition();
           }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addPosition() {
      if (this.pos.name) {
        //添加职位
        this.postRequest('/system/basic/pos/',this.pos).then(resp => {
          if (resp) {
            this.initPosition();
            this.pos.name = '';
          }
        })
      } else {
        this.$message.error('请输入职位 !');
      }
    },
    doUpdate() {
      this.updatePos.createDate = '';
      this.putRequest('system/basic/pos/',this.updatePos).then(resp => {
        if (resp) {
          this.initPosition();
          this.dialogVisible = false;
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
    deleteMany() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        let ids = '?';
        this.multipleSelection.forEach(item => {
          ids += 'ids=' + item.id + '&';
        })

        this.deleteRequest('/system/basic/pos/' + ids).then(resp => {
          if (resp) {
            this.initPosition();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  mounted() {
    this.initPosition();
  }
}
</script>

<style>
.addPosInput {
  width: 300px;
  margin-right: 8px;
}
.posManaMain {
  margin-top: 12px;
}
.updatePosInput {
  width: 200px;
  margin-left: 8px;
}
</style>